package com.unidue.foguing.evaluator;

import org.apache.uima.UimaContext;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
import org.apache.uima.fit.util.JCasUtil;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;

import com.unidue.foguing.types.ActualSentiment;
import com.unidue.foguing.types.EvaluatedSentiment;

public class Evaluator extends JCasAnnotator_ImplBase{

	private int numberOfDocuments;
	private int numberOfPositivesTweets;
	private int correctDetectedPositivesTweets;
	private int numberOfNegativesTweets;
	private int correctDetectedNegativesTweets;
	private int numberOfNeutralTweets;
	private int correctDetectedNeutralTweets;
	
	private double positiveAccuracy;
	private double negativeAccuracy;
	private double neutralAccuracy;
	private double globalAccuracy;
	
	/**
	 * called before the processing of any document.
	 */
	@Override
	public void initialize(UimaContext context)
			throws ResourceInitializationException {
		super.initialize(context);
		
		// instantiate all the attributes
		
		numberOfDocuments = 0;
		numberOfPositivesTweets = 0;
		correctDetectedPositivesTweets = 0;
		numberOfNegativesTweets = 0;
		correctDetectedNegativesTweets = 0;
		numberOfNeutralTweets = 0;
		correctDetectedNeutralTweets = 0;
		positiveAccuracy = 0.0;
		negativeAccuracy = 0.0;
		neutralAccuracy = 0.0;
		globalAccuracy = 0.0;
	}
	
	/**
	 * called once for each document
	 */
	@Override
	public void process(JCas aJCas) throws AnalysisEngineProcessException {
		numberOfDocuments++;
		EvaluatedSentiment evaluatedSentiment = JCasUtil.selectSingle(aJCas, EvaluatedSentiment.class);
		ActualSentiment sentiment = JCasUtil.selectSingle(aJCas, ActualSentiment.class);
		
		System.out.println("tweet SID : " + sentiment.getSID() + " | tweet UID : " + sentiment.getUID());
		System.out.println(sentiment.getActualSentiment() + " evaluated as " + 
							         evaluatedSentiment.getEvaluatedSentiment());
		if(sentiment.getActualSentiment().equals("positive")){
			numberOfPositivesTweets++;
		}
		if(sentiment.getActualSentiment().equals("positive") && sentiment.getActualSentiment().equals(evaluatedSentiment.getEvaluatedSentiment())){
			correctDetectedPositivesTweets++;
		}
		if(sentiment.getActualSentiment().equals("negative")){
			numberOfNegativesTweets++;
		}
		if(sentiment.getActualSentiment().equals("negative") && sentiment.getActualSentiment().equals(evaluatedSentiment.getEvaluatedSentiment())){
			correctDetectedNegativesTweets++;
		}
		if(sentiment.getActualSentiment().equals("objective") || sentiment.getActualSentiment().equals("objective-OR-neutral")
				|| sentiment.getActualSentiment().equals("neutral") ){
			numberOfNeutralTweets++;
		}
		if((sentiment.getActualSentiment().equals("objective") || sentiment.getActualSentiment().equals("objective-OR-neutral")
				|| sentiment.getActualSentiment().equals("neutral")) && evaluatedSentiment.getEvaluatedSentiment().equals("neutral")){
			correctDetectedNeutralTweets++;
		}
		
	}

	/**
	 * called after all documents have been processed
	 */
	@Override
	public void collectionProcessComplete()
			throws AnalysisEngineProcessException {
		// the sum of all correct detected tweets
		super.collectionProcessComplete();
		int sum = correctDetectedNegativesTweets + correctDetectedNeutralTweets + correctDetectedPositivesTweets;
		System.out.println("\n================================================================="); // this line just separate the program output from the evaluation ouptut
		System.err.println("the number total of documents is : " + numberOfDocuments);
		System.err.println("\nnumber of positive tweets : " + numberOfPositivesTweets);
		System.err.println("number of negative tweets : " + numberOfNegativesTweets);
		System.err.println("number of neutral tweets : " + numberOfNeutralTweets);
		
		System.err.println("\npositive tweets detected : " + correctDetectedPositivesTweets);
		System.err.println("negative tweets detected : " + correctDetectedNegativesTweets);
		System.err.println("neutral tweets detected : " + correctDetectedNeutralTweets);
		
		try {
			positiveAccuracy = (double)((correctDetectedPositivesTweets*100)/numberOfPositivesTweets);
			System.err.println("\nthe detection Accuracy of positive tweets is " + positiveAccuracy + "%");
		} catch (ArithmeticException e) {
			throw new AnalysisEngineProcessException();
		}
		
		try {
			negativeAccuracy = (double)((correctDetectedNegativesTweets*100)/numberOfNegativesTweets);
			System.err.println("the detection Accuracy of negative tweets is " + negativeAccuracy + "%");
		} catch (ArithmeticException e) {
			throw new AnalysisEngineProcessException();
		}
		
		try {
			neutralAccuracy = (double)((correctDetectedNeutralTweets * 100)/numberOfNeutralTweets);
			System.err.println("the detection Accuracy of neutral tweets is " + neutralAccuracy + "%");
		} catch (ArithmeticException e) {
			throw new AnalysisEngineProcessException();
		}
		
		System.err.println("\n" + sum + " out of " + numberOfDocuments + " was evaluated correctly.");
		
		try {
			globalAccuracy = (double)((sum*100)/numberOfDocuments);
			System.err.println("the global accuracy of the program is : " + globalAccuracy + "%");
		} catch (ArithmeticException e) {
			throw new AnalysisEngineProcessException();
		}
		
		
	}
}
